
from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class AddContactsPage(BasePage):
    url = "/crm/index.php?m=contacts&a=add"
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    name_locator = (By.ID, 'name')  # 姓名
    saltname_locator = (By.ID, 'saltname')  # 尊称
    customer_name_locator = (By.ID, 'customer_name')  # 所属客户
    fields_locator = (By.ID, 'fields')  # 筛选条件
    searchs_locator = (By.ID, 'searchs')  # 搜索关键词
    search_button_locator = (By.XPATH, '//*[@id="dialog-message2"]/div/ul/li[4]/button')  # 搜索按钮
    choose_locator = (By.XPATH, '//*[@id="datas"]/tr/td[1]/input[1]')  # 选择客户
    queren_locator = (By.XPATH, '/html/body/div[7]/div[3]/div/button[1]')  # 确认按钮
    position_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/form/table/tbody/tr[3]/td[4]/input')  # 职位
    telephone_locator = (By.NAME, 'telephone')  # 电话
    email_locator = (By.NAME, 'email')  # 邮件
    qq_no_locator = (By.NAME, 'qq_no')  # QQ
    zip_code_locator = (By.NAME, 'zip_code')  # 邮编
    address_locator = (By.NAME, 'address')  # 联系地址
    description_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/form/table/tbody/tr[7]/td[2]/textarea')  # 备注
    save_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/form/table/tfoot/tr/td[2]/input[1]')  # 保存
    delete_locator=(By.XPATH,'// *[ @ id = "delete"]')
    first_clue_locator=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[1]/input')
    recycle_bin_locator=(By.XPATH,'/html/body/div[5]/p/a[7]')
    save_new_locator=(By.XPATH,"/html/body/div[5]/div[2]/div/form/table/tfoot/tr/td[2]/input[2]")
    restore_contacts_locator=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[12]/a[2]')
    def input_name(self, contact_name):
        """
        输入联系人姓名
        """
        self.find_element(self.name_locator).send_keys(contact_name)

    def input_salt_name(self, salt_name):
        """
        输入尊称
        """
        self.find_element(self.saltname_locator).send_keys(salt_name)

    def click_customer_name(self):
        """
        点击所属客户
        """
        self.find_element(self.customer_name_locator).click()

    def select_fields(self, fields_value):
        """
        选择筛选条件
        """
        fields_element = Select(self.find_element(self.fields_locator))
        fields_element.select_by_value(fields_value)

    def input_searchs(self, searchs):
        """
        输入搜索关键词
        """
        self.find_element(self.searchs_locator).send_keys(searchs)

    def click_search_btn(self):
        """
        点击搜索按钮
        """
        self.find_element(self.search_button_locator).click()

    def click_choose(self):
        """
        点击选择客户
        """
        self.find_element(self.choose_locator).click()

    def click_queren(self):
        """
        点击确认键
        """
        self.find_element(self.queren_locator).click()

    def input_position(self, position):
        """
        输入职位
        """
        self.find_element(self.position_locator).send_keys(position)

    def input_telephone(self, telephone):
        """
        输入电话
        """
        self.find_element(self.telephone_locator).send_keys(telephone)

    def input_email(self, email):
        """
        输入邮箱
        """
        self.find_element(self.email_locator).send_keys(email)

    def input_qq_no(self, qq):
        """
        输入qq
        """
        self.find_element(self.qq_no_locator).send_keys(qq)

    def input_zip_code(self, zip_code):
        """
        输入邮编
        """
        self.find_element(self.zip_code_locator).send_keys(zip_code)

    def input_address(self, address):
        """
        输入联系地址
        """
        self.find_element(self.address_locator).send_keys(address)

    def input_description(self, description):
        """
        输入备注
        """
        self.find_element(self.description_locator).send_keys(description)

    def click_save(self):
        """
        点击保存
        """
        self.find_element(self.save_locator).click()

    def click_save_new(self):
        """
        点击保存并新建
        """
        self.find_element(self.save_new_locator).click()

    def click_delete(self):
        """
        点击删除
        """
        self.find_element(self.delete_locator).click()

    def click_first_contacts(self):
        """
        选择第一个联系人
        """
        self.find_element(self.first_clue_locator).click()

    def click_recycle_bin(self):
        """
        点击回收站
        """
        self.find_element(self.recycle_bin_locator).click()

    def click_restore_contacts(self):
        """
        点击还原
        """
        self.find_element(self.restore_contacts_locator).click()

#返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

#返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text

#返回报错弹窗文本信息
    def get_error_text(self):
        return self.find_element(self.error_locator).text

